<?php
class Pages{
	public static $naviTemplates = array();
	
	public static function outputPage($pageId = 1) {
		if(!is_numeric($pageId)) exit;
		
		$links = Pages::getPageLinks();
		$allowedFiles = array();
		foreach($links as $data) {
			$allowedFiles[] = $data['linkFile'];
		}
		
		$page = array();
		$query = DB::getInstance()->query("
			SELECT
				".Tables::$pageTbl.".id as pageId,
				".Tables::$pageTbl.".pageName,
				".Tables::$pageBoxTbl.".boxName,
				".Tables::$pageBoxTbl.".id as boxId
			FROM
				".Tables::$pageBoxTbl.",
				".Tables::$pageTbl."
			WHERE
				".Tables::$pageBoxTbl.".pageId = '".$pageId."' &&
				".Tables::$pageTbl.".id = '".$pageId."'
			GROUP BY
				".Tables::$pageBoxTbl.".id
			ORDER BY
				".Tables::$pageBoxTbl.".orderId ASC");
		while($p = DB::getInstance()->fetchAssoc($query)) {
			$page['name'] = $p['pageName'];
			
			// Get Content
			$box = array("boxName" => $p['boxName']);
			$conQuery = DB::getInstance()->query("
				SELECT
					id,
					conTitle,
					conText,
					conFile,
					conFileGetvars
				FROM
					".Tables::$pageBoxContentTbl."
				WHERE
					boxId = '".$p['boxId']."'
				ORDER BY
					orderId ASC");
			while($c = DB::getInstance()->fetchAssoc($conQuery)) {
				if($c['conFile'] == "") {
					$parsedText = BBCode::getInstance()->parse(nl2br($c['conText']),true);
					$parsedText = Smilies::getInstance()->parse($parsedText);
					$box['content'][] = array("conTitle" => $c['conTitle'],"conText" => $parsedText);
				}else{
					// Get File-Content
					if(!empty($c['conFileGetvars'])) {
						// extract Get-Vars
						$getArray = explode("&",$c['conFileGetvars']);
						foreach ($getArray as $key=>$value) {
							$_GET[substr($value,0,strpos($value,'='))] = substr($value,strpos($value,'=') + 1);
						}
					}
					if(file_exists($c['conFile']) && in_array($c['conFile'],$allowedFiles)) {
						include($c['conFile']);
						$content = General::getTpl()->get_template_vars('body');
					}else{
						$content = "Datei nicht vorhanden";
					}
					
					$box['content'][] = array("conFile" => $content);
					
					if(!empty($c['conFileGetvars'])) {
						// delete Get-Vars
						$getArray = explode("&",$c['conFileGetvars']);
						foreach ($getArray as $key=>$value) {
							$_GET[substr($value,0,strpos($value,'='))] = "";
						}
					}
				}
			}
			if(isset($box['content'])) $box['width'] = (100/count($box['content']));
			$page['boxes'][] = $box;
			
			General::getTpl()->assign('extraTitle',$p['pageName']);
		}
		General::getTpl()->assign('page',$page);
		
		General::getTpl()->assign('body',
			General::getTpl()->fetch('page.html'));
	}
	
	
	public static function getPages() {
		$query = DB::getInstance()->query("
			SELECT
				id,
				pageName
			FROM
				".Tables::$pageTbl."");
		$pages = array();
		while($p = DB::getInstance()->fetchAssoc($query)) {
			$pages[] = $p;
		}
		return $pages;
	}
	
	
	public static function createPage($name) {
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$pageTbl."
			SET
				pageName = '".DB::parseVar($name)."'");
	}
	
	public static function editPage($id,$name) {
		if(!is_numeric($id)) exit;
		
		DB::getInstance()->query("
			UPDATE
				".Tables::$pageTbl."
			SET
				pageName = '".DB::parseVar($name)."'
			WHERE
				id = '".$id."'");
	}
	
	public static function deletePage($id) {
		if(!is_numeric($id)) exit;
		
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$pageTbl."
			WHERE
				id = '".$id."'");
		
		// Delete Content
		$boxes = Pages::getBoxesByPageId($id);
		foreach($boxes as $data) {
			DB::getInstance()->query("
				DELETE FROM
					".Tables::$pageBoxContentTbl."
				WHERE
					boxId = '".$data['id']."'");
		}
		
		// Delete Box
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$pageBoxTbl."
			WHERE
				pageId = '".$id."'");
	}
	
	
	
	public static function getBoxesByPageId($id) {
		if(!is_numeric($id)) exit;
		
		$query = DB::getInstance()->query("
			SELECT
				id,
				boxName,
				orderId
			FROM
				".Tables::$pageBoxTbl."
			WHERE
				pageId = '".$id."'
			ORDER BY
				orderId ASC");
		$boxes = array();
		while($b = DB::getInstance()->fetchAssoc($query)) {
			$boxes[] = $b;
		}
		return $boxes;
	}
	
	public static function createBox($pageId,$name) {
		if(!is_numeric($pageId)) exit;
		
		// Get Order-Id
		$query = DB::getInstance()->query("
			SELECT
				id
			FROM
				".Tables::$pageBoxTbl."
			WHERE
				pageId = '".$pageId."'");
		$orderId = DB::getInstance()->numRows($query);
		
		
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$pageBoxTbl."
			SET
				boxName = '".DB::parseVar($name)."',
				pageId = '".$pageId."',
				orderId = '".$orderId."'");
	}
	
	public static function editBox($boxId,$pageId,$name) {
		if(!is_numeric($boxId) || !is_numeric($pageId)) exit;
		
		DB::getInstance()->query("
			UPDATE
				".Tables::$pageBoxTbl."
			SET
				pageId = '".$pageId."',
				boxName = '".DB::parseVar($name)."'
			WHERE
				id = '".$boxId."'");
	}
	
	public static function deleteBox($boxId) {
		if(!is_numeric($boxId)) exit;
		
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$pageBoxContentTbl."
			WHERE
				boxId = '".$boxId."'");
		
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$pageBoxTbl."
			WHERE
				id = '".$boxId."'");
	}
	
	
	public static function getBoxContent($boxId) {
		if(!is_numeric($boxId)) exit;
		
		$query = DB::getInstance()->query("
			SELECT
				id,
				conTitle,
				conText,
				conFile,
				conFileGetvars,
				orderId
			FROM
				".Tables::$pageBoxContentTbl."
			WHERE
				boxId = '".$boxId."'
			ORDER BY
				orderId ASC");
		$content = array();
		while($c = DB::getInstance()->fetchAssoc($query)) {
			$content[] = $c;
		}
		return $content;
	}
	
	public static function createBoxContent($boxId,$title,$text,$file,$getVars) {
		if(!is_numeric($boxId)) exit;
		
		// Get Order-Id
		$query = DB::getInstance()->query("
			SELECT
				id
			FROM
				".Tables::$pageBoxContentTbl."
			WHERE
				boxId = '".$boxId."'");
		$orderId = DB::getInstance()->numRows($query);
		
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$pageBoxContentTbl."
			SET
				boxId = '".$boxId."',
				conTitle = '".DB::parseVar($title)."',
				conText = '".DB::parseVar(htmlspecialchars($text))."',
				conFile = '".DB::parseVar($file)."',
				conFileGetvars = '".DB::parseVar($getVars)."',
				orderId = '".$orderId."'");
	}
	
	public static function editBoxContent($id,$boxId,$title,$text,$file,$getVars) {
		if(!is_numeric($boxId) || !is_numeric($id)) exit;
		
		DB::getInstance()->query("
			UPDATE
				".Tables::$pageBoxContentTbl."
			SET
				boxId = '".$boxId."',
				conTitle = '".DB::parseVar($title)."',
				conText = '".DB::parseVar(htmlspecialchars($text))."',
				conFile = '".DB::parseVar($file)."',
				conFileGetvars = '".DB::parseVar($getVars)."'
			WHERE
				id = '".$id."'");
	}
	
	public static function deleteBoxContent($id) {
		if(!is_numeric($id)) exit;
		
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$pageBoxContentTbl."
			WHERE
				id = '".$id."'");
	}
	
	/**
	 * Box-Links
	 */
	public static function addPageLink($connId,$name,$file,$get) {
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$pageLinksTbl."
			SET
				linkConnectionId = '".DB::parseVar($connId)."',
				linkName = '".DB::parseVar($name)."',
				linkFile = '".DB::parseVar($file)."',
				linkGetParameters = '".DB::parseVar($get)."'");
	}
	public static function editPageLink($connId,$name,$file,$get) {
		DB::getInstance()->query("
			UPDATE
				".Tables::$pageLinksTbl."
			SET
				linkName = '".DB::parseVar($name)."',
				linkFile = '".DB::parseVar($file)."',
				linkGetParameters = '".DB::parseVar($get)."'
			WHERE
				linkConnectionId = '".DB::parseVar($connId)."'");
	}
	public static function deletePageLink($connId) {
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$pageLinksTbl."
			WHERE
				linkConnectionId = '".DB::parseVar($connId)."'");
	}
	public static function getPageLinks() {
		$query = DB::getInstance()->query("
			SELECT
				id,
				linkConnectionId,
				linkName,
				linkFile,
				linkGetParameters
			FROM
				".Tables::$pageLinksTbl."");
		$links = array();
		while($l = DB::getInstance()->fetchAssoc($query)) {
			$links[] = $l;
		}
		return $links;
	}
}
?>